{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 03.12.2008 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit CryptQestion;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, LbCipher, LbClass, LbUtils, ChatDistributer;

type
  Tfrm_CryptQuestion = class(TForm)
    pnlQuestion: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    pnlAnswer: TPanel;
    Label6: TLabel;
    Label7: TLabel;

    Label4: TLabel;
    memquestion: TMemo;
    edtAnswer: TEdit;
    Label5: TLabel;
    btnSendIt: TButton;
    Button2: TButton;
    constructor CreateQuestion(Owner: TComponent; cryptProt: TObject);
    constructor CreateAnswer(Owner: TComponent; CryptProt: TObject; Question: string; WrongAnswer: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSendItClick(Sender: TObject);
    procedure edtAnswerKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
  private
    cryptProt: TObject;
    sent: Boolean;
  end;

var
  frm_CryptQuestion: Tfrm_CryptQuestion;

implementation

uses
  CryptoLayer;

{$R *.dfm}

{ Tfrm_CryptQestion }

constructor Tfrm_CryptQuestion.CreateAnswer(Owner: TComponent;
  cryptProt: TObject; Question: string; WrongAnswer: Boolean);
begin
  inherited Create(owner);
  sent := False;
  self.CryptProt := cryptProt;
  Show;
  pnlQuestion.Visible := False;
  pnlAnswer.Visible := True;
  memquestion.Text := Question;
  memquestion.Color := Color;
  memquestion.ReadOnly := True;
  SetForegroundWindow(Application.MainForm.Handle);
  edtAnswer.SetFocus;
end;

constructor Tfrm_CryptQuestion.CreateQuestion(Owner: TComponent; CryptProt: Tobject);
begin
  inherited create(owner);
  self.CryptProt := CryptProt;
  Sent := False;
  Show;
  pnlQuestion.Visible := True;
  pnlAnswer.Visible := False;
  SetForegroundWindow(Application.MainForm.Handle);
  memQuestion.SetFocus;
end;

procedure Tfrm_CryptQuestion.btnSendItClick(Sender: TObject);
var
  s: string;
  md5: TMD5Digest;
begin
  if pnlQuestion.Visible then
  begin
    if memquestion.Text = '' then
      memquestion.Text := '_';
    if cryptProt is TCryptProtocollVersion1 then
    begin
      (cryptProt as TCryptProtocollVersion1).SendQuestion(memquestion.Text);
        sent := true;
    end;
  end;
  StringHashMD5(md5, edtAnswer.Text);
  s := BufferToHex(md5, SizeOf(md5));
  if cryptProt is TCryptProtocollVersion1 then
  begin
    (cryptProt as TCryptProtocollVersion1).SetSecretAnswerMD5(s);
        sent := true;
  end;
  Close;
end;

procedure Tfrm_CryptQuestion.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  if cryptProt is TCryptProtocollVersion1 then
  begin
    (cryptProt as TCryptProtocollVersion1).UnregisterCryptQuesWindow;
    if not sent then
      (cryptProt as TCryptProtocollVersion1).GoToCryptState(csWeak);
  end;
end;

procedure Tfrm_CryptQuestion.edtAnswerKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
  begin
    key := #0;
    btnSendIt.Click;
  end;
end;

procedure Tfrm_CryptQuestion.Button2Click(Sender: TObject);
begin
  Close;
end;

end.
